package br.ita.trucocearense.cliente.core;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.Serializable;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;

import br.ita.trucocearense.common.core.interfaces.remote.UsuarioRemoto;
import br.ita.trucocearense.common.core.interfaces.remoteobjects.ObjUsuario;

public class Usuario{
	private String login;
	private String senha;
	private String apelido;
	private int score;
	private UsuarioRemoto userServer = null;
	private int posicaoMesa;
	
	public Usuario(){		
	}
	
	
	public Usuario(String login, String senha, String apelido) {
		super();
		this.login = login;
		this.senha = senha;
		this.apelido = apelido;
		this.setPosicaoMesa(-1);
	}


	public Usuario(String login, String apelido, int posicaoMesa) {
		super();
		this.login = login;
		this.apelido = apelido;
		this.posicaoMesa = posicaoMesa;
	}


	public int getPosicaoMesa() {
		return posicaoMesa;
	}
	public void setPosicaoMesa(int posicaoMesa) {
		this.posicaoMesa = posicaoMesa;
	}
	public int getScore() {
		return score;
	}
	public void setScore(int score) {
		this.score = score;
	}
	public String getLogin() {
		return login;
	}
	public void setLogin(String login) {
		this.login = login;
	}
	public String getSenha() {
		return senha;
	}
	public void setSenha(String senha) {
		this.senha = senha;
	}
	public String getApelido() {
		return apelido;
	}
	public void setApelido(String apelido) {
		this.apelido = apelido;
	}
	
	
	public boolean fazerLogin(String login,String senha) {
		ObjUsuario newuser = null;
		try {
			newuser = this.getServer().fazerLogin(login, senha);
			if (newuser == null)
				return false;
			this.setApelido(newuser.getApelido());
			this.setLogin(newuser.getLogin());
			this.setScore(newuser.getScore());
			this.setSenha(newuser.getSenha());
			this.setPosicaoMesa(-1);
			return true;
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	
	public void fazerLogout() {
		try {
			this.getServer().fazerLogout(this.getLogin());
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}
	
	public UsuarioRemoto getServer(){
		if (userServer != null)
			return userServer;
		try {			
			System.setSecurityManager(new RMISecurityManager());
			this.userServer = (UsuarioRemoto) Naming.lookup("rmi://" + getIPServer() + "/usuarioremoto");			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return userServer;		
	}
	
	private String getIPServer() {
		try {
			BufferedReader reader =  new BufferedReader(
					new FileReader(System.getProperty("user.dir") + File.separator + "IPServer.txt"));
			return reader.readLine();
		} catch (Exception e) {
			return "localhost";
		}		
	}
	
	public void atualizaScore(){
		try {
			int score = this.getServer().obterScoreAtualizado(this.getLogin());
			this.setScore(score);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public boolean cadastrarUsuario(){
		if (this.getLogin().equals("") || this.getApelido().equals(""))
			return false;
		
		try {
			if (this.getServer().cadastrarUsuario(this.getLogin(), this.getSenha(), this.getApelido())){
				this.setScore(1000);
				return true;
			}
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	
	

}
